package cn.easy.sql.excel;


import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 对输入文件进行解析
 */
public class ExcelAnalyze {

    public static void main(String[] args) throws Exception {
        regularHeaderAnalyze(null,null);
    }


    /**
     * 规则表头
     * 默认第一行是表头，第二行是数据
     * @param file 导入文件
     * @return 数据列表
     * @throws Exception
     */
    public static List<Object> regularHeaderAnalyze(File file,Class<T> itemClass) throws Exception{
        File excelFile = new File("C:\\Users\\11469\\Desktop\\测试.xlsx");
        List<Object> dataItems = new ArrayList<>();

        try (
                FileInputStream fis = new FileInputStream(excelFile);
             Workbook workbook = new XSSFWorkbook(fis)
        ) {

            Sheet sheet = workbook.getSheetAt(0);

            // 读取表头，创建字段映射
            Iterator<Row> rowIterator = sheet.iterator();
            Row headerRow = rowIterator.next();
            Map<Integer, String> headers = new HashMap<>();
            for (Cell cell : headerRow) {
                headers.put(cell.getColumnIndex(), cell.getStringCellValue());
            }

            // 遍历数据行
            while (rowIterator.hasNext()) {
                Row dataRow = rowIterator.next();
                DataItem dataItem = new DataItem();

                // 遍历每个单元格
                for (Cell cell : dataRow) {
                    int columnIndex = cell.getColumnIndex();
                    String headerName = headers.get(columnIndex);
                    if (headerName != null) {
                        // 使用反射调用setField方法设置属性值
                        try {
                            Method setMethod = DataItem.class.getMethod("setField", String.class, Object.class);
                            setMethod.invoke(dataItem, headerName, getCellValue(cell));
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }

                // 将DataItem添加到列表中
                dataItems.add(dataItem);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataItems;
    }

    private static Object getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue();
                } else {
                    return cell.getNumericCellValue();
                }
            case BOOLEAN:
                return cell.getBooleanCellValue();
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }

}
